import {login, logout, getInfo, loginByPhoneCode, loginByQrCode} from '@/api/user'
import {faceLogin, faceUpdate} from '../../api/facelogin'
import {getToken, setToken, removeToken} from '@/utils/auth'
import {resetRouter} from '../../router'

const state = {
    token: getToken(),
    name: '',
    avatar: ''
}

const mutations = {
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_AVATAR: (state, avatar) => {
        state.avatar = avatar
    }
}

const actions = {
    // user login
    login({commit}, userInfo) {
        const {username, password} = userInfo
        return new Promise((resolve, reject) => {
            login({username: username.trim(), password: password}).then(response => {
                const {data} = response
                commit('SET_TOKEN', data.token)
                setToken(data.token)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // face login
    faceLogin({commit}, data) {
        return new Promise((resolve, reject) => {
            faceLogin(data).then(response => {
                const {data, success, message} = response
                if (!success) {
                    reject(message)
                }
                commit('SET_TOKEN', data.token)
                setToken(data.token)
                resolve(message)
            }).catch(error => {
                console.log("error:", error)
                reject(error)
            })
        })
    },

    // phone login
    phoneLogin({commit}, data) {
        return new Promise((resolve, reject) => {
            loginByPhoneCode(data).then(response => {
                const {data, success, message} = response
                if (!success) {
                    reject(message)
                }
                commit('SET_TOKEN', data.token)
                setToken(data.token)
                resolve(message)
            }).catch(error => {
                console.log("error:", error)
                reject(error)
            })
        })
    },

    // qrcode login
    qrcodeLogin({commit}, data) {
        return new Promise((resolve, reject) => {
            loginByQrCode(data).then(response => {
                const {data, success, message} = response
                if (!success) {
                    reject(message)
                }
                commit('SET_TOKEN', data.token)
                setToken(data.token)
                resolve(message)
            }).catch(error => {
                console.log("error:", error)
                reject(error)
            })
        })
    },

    // get user info
    getInfo({commit, state}) {
        return new Promise((resolve, reject) => {
            getInfo(state.token).then(response => {
                const {data} = response

                if (!data) {
                    reject('认证失败，请重试！')
                }

                const {name, avatar} = data

                commit('SET_NAME', name)
                commit('SET_AVATAR', avatar)
                resolve(data)
            })
        })
    },

    // user logout
    logout({commit, state}) {
        return new Promise((resolve, reject) => {
            logout(state.token).then(() => {
                commit('SET_TOKEN', '')
                commit('SET_NAME', '')
                commit('SET_AVATAR', '')
                removeToken()
                resetRouter()
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // remove token
    resetToken({commit}) {
        return new Promise(resolve => {
            commit('SET_TOKEN', '')
            removeToken()
            resolve()
        })
    },

}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}

